<?php 
defined('IN_SITE') or exit('Access Denied');
@set_time_limit(600);
require_once MOD_ROOT.'goods/goods.class.php';
$goods = new goods();
include MOD_ROOT.'template/template.func.php';


if($dosubmit)
{
	require_once MOD_ROOT.'attachment/attachment.class.php';
	require_once CACHE_MODEL_PATH.'goods_output.class.php';
	require_once MOD_ROOT.'goods/goods.class.php';

	$attachment = new attachment($module, $catid);
	$c = new goods();
	$coutput = new goods_output();
	if($type == 'lastinput')
	{
		$offset = 0;
	}
	else
	{
		$page = max(intval($page), 1);
		$offset = $pagesize*($page-1);
	}
	$where = ' WHERE status=99 ';
	$order = 'ASC';
	
	if(!isset($first) && is_array($catids) && $catids[0] > 0) 
	{
		cache_write('html_show_'.$_userid.'.php', $catids);
		$catids = implodeids($catids);
		$where .= " AND catid IN($catids) ";
		$first = 1;
	}
	elseif($first)
	{
		$catids = cache_read('html_show_'.$_userid.'.php');
		$catids = implodeids($catids);
		$where .= " AND catid IN($catids) ";
	}
	else
	{
		$first = 0;
	}

	if($type == 'lastinput' && $number)
	{
		$offset = 0;
		$pagesize = $number;
		$order = 'DESC';
	}
	elseif($type == 'date')
	{
		if($fromdate)
		{
			$fromtime = strtotime($fromdate.' 00:00:00');
			$where .= " AND `inputtime`>=$fromtime ";
		}
		if($todate)
		{
			$totime = strtotime($todate.' 23:59:59');
			$where .= " AND `inputtime`<=$totime ";
		}
	}
	elseif($type == 'id')
	{
		$fromid = intval($fromid);
		$toid = intval($toid);
		if($fromid) $where .= " AND `goodsid`>=$fromid ";
		if($toid) $where .= " AND `goodsid`<=$toid ";
	}
	if(!isset($total) && $type != 'lastinput')
	{
		$total = cache_count("SELECT COUNT(*) AS `count` FROM `".DB_PRE."goods` $where");
		$pages = ceil($total/$pagesize);
		$start = 1;
	}
	$data = $db->select("SELECT `goodsid` FROM `".DB_PRE."goods` $where ORDER BY `goodsid` $order LIMIT $offset,$pagesize");
	foreach($data as $r)
	{
		$goods->html($r['goodsid']);
	}
	if($pages > $page)
	{
		$page++;
		$creatednum = $offset + count($data);
		$percent = round($creatednum/$total, 2)*100;
		$message = "共需更新 <font color='red'>$total</font> 条信息<br />已完成 <font color='red'>{$creatednum}</font> 条（<font color='red'>{$percent}%</font>）";
		$forward = $start ? "?module=$module&file=$file&type=$type&dosubmit=1&first=$first&action=$action&fromid=$fromid&toid=$toid&fromdate=$fromdate&todate=$todate&pagesize=$pagesize&page=$page&pages=$pages&total=$total" : preg_replace("/&page=([0-9]+)&pages=([0-9]+)&total=([0-9]+)/", "&page=$page&pages=$pages&total=$total", URL);;
	}
	else
	{
		cache_delete('html_show_'.$_userid.'.php');
		$message = "更新完成！";
		$forward = "?module=$module&file=$file";
	}
	showmessage($message, $forward);
}
else
{
	include ('templates/html.tpl.php');
}
?>